﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XNA_Game_Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace Basic_XNA
{
	public class MainMenu : Screen
	{
		Container mainWindow;
		WindowVar dialogue;
		MainGame runningGame;
		SpriteFont stFont;

		public MainMenu(Game game, ScreenManager screenManager)
			: base(game, screenManager, "MenuScreen")
		{
			CreateThemes();
			CreateScreen();
			CreateDialogueBox();
			ScreenwideInputhandling();
		}

		private void CreateThemes()
		{
			stFont = Content.Load<SpriteFont>(@"testFont");

			Texture2D pointerTexture = LoadTexture2D("pointer");
			Texture2D pointerBody = LoadTexture2D("pointerbody");

			XNA_Game_Framework.Theme.Pointer pointer = Themes.AddPointerTheme("pointer", pointerTexture, pointerBody, pointerTexture);

			Texture2D border = LoadTexture2D("blackdot");
			Texture2D smallborder = LoadTexture2D("smallblackdot");

			XNA_Game_Framework.Theme.Border borderTheme = Themes.AddBorderTheme("border", border, border, border, border, border, border, border, border);
			XNA_Game_Framework.Theme.Border thinBorderTheme = Themes.AddBorderTheme("thinborder", smallborder, smallborder, smallborder, smallborder, smallborder, smallborder, smallborder, smallborder);

			Texture2D buttonTexture = LoadTexture2D("button");

			XNA_Game_Framework.Theme.Button buttonTheme = Themes.AddButtonTheme("button", buttonTexture, stFont);
			Themes.AddButtonTheme("yesButton", buttonTexture, stFont, "Yes");
			Themes.AddButtonTheme("noButton", buttonTexture, stFont, "No");

			Texture2D smallbackground = LoadTexture2D("smallwhitedot");

			Themes.AddTextBoxTheme("textBox", thinBorderTheme, pointer, smallbackground, stFont);

			Texture2D background = LoadTexture2D("whitedot");

			Themes.AddDialogueTheme("dialogue", borderTheme, buttonTheme, stFont, background);

			Texture2D selectionBox = LoadTexture2D("choicebox");

			Themes.AddMenuTheme("menu", stFont, selectionBox);

			Texture2D headertopleft = LoadTexture2D("topleftheader");
			Texture2D headertopright = LoadTexture2D("toprightheader");
			Texture2D headerbottom = LoadTexture2D("headerbottomborder");
			Texture2D headerbottomleft = LoadTexture2D("headerbottomleft");
			Texture2D headerbottomright = LoadTexture2D("headerbottomright");
			Texture2D headerborder = LoadTexture2D("headerborder");
			Texture2D headerbackground = LoadTexture2D("greydot");

			XNA_Game_Framework.Theme.Border headerBorderTheme = Themes.AddBorderTheme("", headertopleft, headertopright, headerbottomleft, headerbottomright, headerborder, headerborder, headerborder, headerbottom);

			Themes.AddWindowVarTheme("windowTest", stFont, headerBorderTheme, headerbackground, borderTheme, background);
		}

		private void CreateScreen()
		{
			Texture2D choiceBox = Content.Load<Texture2D>(@"choicebox");
			Texture2D background = Content.Load<Texture2D>(@"space-background");

			mainWindow = this.AddContainer("wndMain", new Vector2(0, 0), new Vector2(800, 480));
			
			mainWindow.AddImage("imgBackground", new Vector2(0, 0), background);

			Menu menu = mainWindow.AddMenu("mnuMain", new Vector2(250, 100), new string[] { "New Game", "Resume Game", "Exit" }, "menu", true);
			menu.GetMenuItem(0).Click += NewGame;
			menu.GetMenuItem(1).Click += LoadGame;
			menu.GetMenuItem(2).Click += RevealExitDialogue;
		}

		private void CreateDialogueBox()
		{
			dialogue = this.AddWindowVar("wndDialogue", "Do you really want to quit?", new Vector2(270, 100), new Vector2(260, 132), "windowTest");
			Container content = dialogue.Container;

			dialogue.Enabled = false;
			dialogue.Visible = false;

			Button yesButton = content.AddButton("btnYes", new Vector2(20, 20), "yesButton");
			yesButton.Click += ExitGame;
			Button noButton = content.AddButton("btnNo", new Vector2(140, 20), "noButton");
			noButton.Click += HideExitDialogue;
		}

		private void ScreenwideInputhandling()
		{
			this.InputHandler.EscKeyPressed += new InputHandler.KeyPressedEventHandler(ExitPressed);
			this.InputHandler.YKeyPressed += new InputHandler.KeyPressedEventHandler(Y_Pressed);
			this.InputHandler.NKeyPressed += new InputHandler.KeyPressedEventHandler(N_Pressed);
		}

		private void NewGame(object sender, EventArgs e)
		{
			runningGame = (MainGame)myScreenManager.AddScreen(typeof(MainGame));
		}

		private void LoadGame(object sender, EventArgs e)
		{
			if (runningGame != null)
			{
				myScreenManager.AddExistingScreen(runningGame);
			}
		}

		private void RevealExitDialogue(object sender, EventArgs e)
		{
			dialogue.Enabled = true;
			dialogue.Visible = true; 
		}

		private void HideExitDialogue(object sender, EventArgs e)
		{
			dialogue.Enabled = false;
			dialogue.Visible = false;
		}

		private void ExitGame(object sender, EventArgs e)
		{
			gameRef.Exit();
		}

		private void ExitPressed(object sender, KeyPressedEventArgs e)
		{
			if (Enabled)
			{
				RevealExitDialogue(null, new EventArgs());
			}
		}

		private void Y_Pressed(object sender, KeyPressedEventArgs e)
		{
			if (dialogue.Enabled)
			{
				ExitGame(null, new EventArgs());
			}
		}

		private void N_Pressed(object sender, KeyPressedEventArgs e)
		{
			if (dialogue.Enabled)
			{
				HideExitDialogue(null, new EventArgs());
			}
		}
	}
}
